Using the calc() Function to Adjust for Padding and Borders in the Box Model
The CSS calc() function allows you to perform calculations directly within property values, making it useful for compensating for padding or border widths when using the default content-box model. It helps you maintain precise layout control without manually subtracting fixed values.
In this example, the calc() function subtracts the total horizontal padding and border from 100% of the container width. This ensures the element fits perfectly inside its container even with the default content-box box model.
When using content-box and you want to preserve precise widths including padding and borders.
When building fluid layouts that must account for dynamic container widths.
When mixing fixed and flexible units (e.g., percentages and pixels).
Here, calc() ensures the sidebar occupies half of the container width while compensating for internal spacing. This is especially helpful in responsive layouts that rely on flexible widths.
calc() helps maintain precise sizing in the content-box model by adjusting for padding and border widths.
You can mix units freely inside calc() (e.g., percentages, pixels, ems).
Using box-sizing: border-box is usually simpler, but calc() gives more flexibility when exact control is needed.